package com.yzc.android.ui.base.fragment;

import java.util.ArrayList;
import java.util.List;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.yzc.android.utils.MapUtils;

public class DriveTraceOverlay extends OverlayManager {

    private List<BDLocation> mBDLocations = new ArrayList<>();

    public DriveTraceOverlay(BaiduMap baiduMap) {
        super(baiduMap);
    }

    @Override
    public List<OverlayOptions> getOverlayOptions() {
        if (mBDLocations == null || mBDLocations.size() < 2)
        {
            return null;
        }
        List<OverlayOptions> result = new ArrayList<>();
        List<LatLng> points = new ArrayList<>();
        for (BDLocation location : mBDLocations) {
            points.add(new LatLng(location.getLatitude(), location.getLongitude()));
        }
        MapUtils.scaleByPoints(mBaiduMap, points, points.get(points.size() - 1), true);

        OverlayOptions ooPolyline = new PolylineOptions().width(10).color(0xFF4aba4c).points(points);
        result.add(ooPolyline);
        return result;
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }

    @Override
    public boolean onPolylineClick(Polyline polyline) {
        return false;
    }

    public void addPoint(BDLocation bdLocation) {
        if (!mBDLocations.contains(bdLocation)) {
            mBDLocations.add(bdLocation);
            addToMap();
        }
    }

    public void removePoint(BDLocation bdLocation) {
        if (mBDLocations.contains(bdLocation)) {
            mBDLocations.remove(bdLocation);
            addToMap();
        }
    }

    public void clearPoint() {
        mBDLocations.clear();
        removeFromMap();
    }
}